package accs.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import accs.gui.logic.CheckerTask;
import accs.model.mail.cracker.MailCracker;
import accs.model.mail.cracker.MailCrackerTask;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class MailCrackerDialog extends JDialog {
	private JTextField mailField;
	private JTextField passFileField;
	
	private boolean result=false;
	private JSpinner threadSpinner;
	
	public MailCrackerDialog(JFrame owner) {
		super(owner,true);
		setTitle("MailCracker");
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		getContentPane().setLayout(new MigLayout("", "[][grow]", "[][][][]"));
		
		addComponentListener(new ComponentAdapter() {
			public void componentShown(ComponentEvent arg0) {
				result=false;
			}
		});
		
		JLabel lblEmailaddress = new JLabel("e-Mail-Address:");
		getContentPane().add(lblEmailaddress, "cell 0 0,alignx trailing");
		
		mailField = new JTextField();
		getContentPane().add(mailField, "cell 1 0,growx");
		mailField.setColumns(10);
		
		JLabel lblPasswordfile = new JLabel("Password-File:");
		getContentPane().add(lblPasswordfile, "cell 0 1,alignx trailing");
		
		passFileField = new JTextField();
		getContentPane().add(passFileField, "flowx,cell 1 1,growx");
		passFileField.setColumns(10);
		
		JButton button = new JButton("...");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser=new JFileChooser();
				chooser.setSelectedFile(new File(passFileField.getText()));
				chooser.showOpenDialog(MailCrackerDialog.this);
				
				passFileField.setText(chooser.getSelectedFile().getAbsolutePath());
			}
		});
		getContentPane().add(button, "cell 1 1");
		
		JButton btnNewButton = new JButton("Ok");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				result=true;
				setVisible(false);
			}
		});
		
		JLabel lblThreads = new JLabel("Threads:");
		getContentPane().add(lblThreads, "cell 0 2,alignx right");
		
		threadSpinner = new JSpinner();
		threadSpinner.setModel(new SpinnerNumberModel(1, 1, 50, 1));
		getContentPane().add(threadSpinner, "cell 1 2,growx");
		getContentPane().add(btnNewButton, "flowx,cell 1 3,alignx right");
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				result=false;
				setVisible(false);
			}
		});
		getContentPane().add(btnCancel, "cell 1 3");
		
		pack();
		setLocationRelativeTo(owner);
	}
	
	public boolean getResult() {
		return result;
	}
	
	public CheckerTask getTask() throws IOException {
		MailCrackerTask task=new MailCrackerTask();
		task.setEmail(mailField.getText());
		task.setPasswords(MailCracker.readPassword(new File(passFileField.getText())));
		task.setThreadNumber((Integer)threadSpinner.getValue());
		
		return task;
	}
}